package com.render.game;

import java.util.ArrayList;
import java.util.HashMap;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.engine.game.GameEntity;
import com.engine.game.GameObject;

public class Stage {
	
	private ArrayList<String> Background;
	private ArrayList<String> Decorations;
	private ArrayList<String> NPC;
	private ArrayList<String> Player;
	private ArrayList<String> Interface;
	
	private Vector2 viewportMin;
	private Vector2 viewportMax;
	
	private Vector2 screenMinInWorld;
	private Vector2 screenMaxInWorld;
	
	
	
	
	public Stage(Vector2 viewportMin, Vector2 viewportMax, Vector2 screenMinInWorld, Vector2 screenMaxInWorld ){
		this.viewportMax = viewportMax;
		this.viewportMin = viewportMin;
		
		this.screenMinInWorld = screenMinInWorld;
		this.screenMaxInWorld = screenMaxInWorld;
		
		this.Background = new ArrayList<String>();
		this.Decorations = new ArrayList<String>();
		this.NPC = new ArrayList<String>();
		this.Player = new ArrayList<String>();
		this.Interface = new ArrayList<String>();
	}
	
	public void pan(Vector2 pan){
		this.screenMinInWorld = this.screenMinInWorld.add(pan);
		this.screenMaxInWorld = this.screenMaxInWorld.add(pan);
	}
	
	public void zoom(Vector2 zoom){
		this.screenMinInWorld = this.screenMinInWorld.add(zoom);
		this.screenMaxInWorld = this.screenMaxInWorld.sub(zoom);
	}
	
	public Vector2 getScreenMinInWorld(){
		return this.screenMinInWorld;
	}
	
	public Vector2 getScreenMaxInWorld(){
		return this.screenMaxInWorld;
	}
	
	public Vector2 getViewportMin(){
		return this.viewportMin;
	}
	
	public Vector2 getViewportMax(){
		return this.viewportMax;
	}
	
	
	public void changeViewport(Vector2 min, Vector2 max){
		viewportMin.set(min);
		viewportMax.set(max);
	}
	
	public void setFullscreen(){
		viewportMax.set(new Vector2(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
		viewportMin.set(0,0);
	}
	
	public void clear(){
		this.Background.clear();
		this.Decorations.clear();
		this.NPC.clear();
		this.Player.clear();
		this.Interface.clear();
		
		
	}
	
	public void pushObject(String name, RenderType type){
		switch(type){
		case background:
			this.Background.add(name);
			break;
		case decoration:
			this.Decorations.add(name);
			break;
		case gui:
			this.Interface.add(name);
			break;
		case npc:
			this.NPC.add(name);
			break;
		case player:
			this.Player.add(name);
			break;
		default:
			break;
		
		}
	}
	
	public void pushObject(String name, RenderType type, int index){
		switch(type){
		case background:
			this.Background.add(index, name);
			break;
		case decoration:
			this.Decorations.add(index, name);
			break;
		case gui:
			this.Interface.add(index, name);
			break;
		case npc:
			this.NPC.add(index, name);
			break;
		case player:
			this.Player.add(index, name);
			break;
		default:
			break;
		
		}
	}
	
	public void deleteObject(String name, RenderType type){
		switch(type){
		case background:
			this.Background.remove(name);
			break;
		case decoration:
			this.Decorations.remove(name);
			break;
		case gui:
			this.Interface.remove(name);
			break;
		case npc:
			this.NPC.remove(name);
			break;
		case player:
			this.Player.remove(name);
			break;
		default:
			break;
		
		}
	}
	
	public void render(HashMap<String, GameEntity> mapOfGameObjects, SpriteBatch batch, float alpha){
		
		
		
		
		Gdx.gl.glViewport((int)viewportMin.x, (int)viewportMin.y, (int)viewportMax.x, (int)viewportMax.y);
		
		
		Sprite sprite;
		
		for(String name : Background){
			
			sprite = ((GameObject) mapOfGameObjects.get(name)).getRender().getSprite();
			sprite.draw(batch);
		}
		
		for(String name : Decorations){
			sprite = ((GameObject) mapOfGameObjects.get(name)).getRender().getSprite();
			sprite.draw(batch);
		}
		
		for(String name : NPC){
			GameObject object = (GameObject) mapOfGameObjects.get(name);
			
			if(object.isMovable){
				object.lerp(alpha);
			}
			
			sprite = object.getRender().getSprite();
			sprite.draw(batch);
			
			
		}
		
		for(String name : Player){
            GameObject object = (GameObject) mapOfGameObjects.get(name);
			
			if(object.isMovable){
				object.lerp(alpha);
			}
			
			sprite = object.getRender().getSprite();
			sprite.draw(batch);
			
			
		}
		
		for(String name : Interface){
			sprite = ((GameObject) mapOfGameObjects.get(name)).getRender().getSprite();
			sprite.draw(batch);
			
		}
		
		
	}
	
}
